//
//  LXSlideSelectionView.m
//  ibgs_patients
//
//  Created by xiao.zhao@lachesis-mh.com on 15/12/30.
//  Copyright © 2015年 xiao.zhao@lachesis-mh.com. All rights reserved.
//

#import "LXSlideSelectionView.h"

#define kSlideButtonBaseTag 1000

@interface LXSlideSelectionView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation LXSlideSelectionView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self setup];
    }
    
    return self;
}

-(void)setup
{
    _topScrollView = [[UIScrollView alloc]init];
    _topScrollView.pagingEnabled = YES;
    _topScrollView.showsHorizontalScrollIndicator = NO;
    _topScrollView.showsVerticalScrollIndicator = NO;
    [self addSubview:_topScrollView];
    
    _mainScrollView = [[UIScrollView alloc]init];
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.bounces = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.delegate = self;
    [self addSubview:_mainScrollView];
    
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = kAppGrayTextColor;
    [self addSubview:_lineView];
    
    self.textFont = [UIFont lx_systemFontOfSize:17];
    self.maxEqualCount = 3;
    self.lineWidth = 3.0f;
    self.lineColor = kAppLinesColor;
    self.selectedIndex = 0;
    
    self.layer.shadowColor = kAppNormalTextColor.CGColor;
    self.layer.shadowOffset = CGSizeMake(2, 5);
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowRadius = 5;
}

-(void)buildUI
{
    CGRect frect = CGRectZero;
    CGSize boundingSize = CGSizeMake(1000.0f, 100.0f);
    CGFloat startX = 0;
    CGFloat itemWidth = kScreenWidth / _viewControllers.count;
    
    for (UIViewController *vc in _viewControllers) {
        NSUInteger index = [_viewControllers indexOfObject:vc];
        
        NSString *title = vc.title;
        
        frect.origin.x = startX;
        startX += itemWidth;
        
        frect.size.width = itemWidth;
        frect.size.height = _topViewHeight;
        UIButton *btn = [[UIButton alloc]initWithFrame:frect];
        [btn setTitleColor:kAppNormalTextColor forState:UIControlStateNormal];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(topAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTag:kSlideButtonBaseTag + index];
        [btn.titleLabel setFont:_textFont];
        [_topScrollView addSubview:btn];
        
        //main
        [_mainScrollView addSubview:vc.view];
    }
    
    frect = CGRectZero;
    frect.origin.x = 0;
    frect.origin.y = _topViewHeight - self.lineWidth;
    frect.size.width = itemWidth;
    frect.size.height = self.lineWidth;
    _topLineView = [[UIView alloc] initWithFrame:frect];
    _topLineView.backgroundColor = kAppGrayTextColor;
    [_topScrollView addSubview:_topLineView];
    
    _topScrollView.contentSize = CGSizeMake(startX, _topScrollView.bounds.size.height);
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.bounds.size.width * _viewControllers.count, _mainScrollView.bounds.size.height);
}

- (void)setTopViewHeight:(NSInteger)topViewHeight
{
    _topViewHeight = topViewHeight;
}

- (void)setTextFont:(UIFont *)textFont
{
    _textFont = textFont;
}

-(void)layoutSubviews
{
    _topScrollView.frame = CGRectMake(0, 0, self.bounds.size.width, _topViewHeight);
    _mainScrollView.frame = CGRectMake(0, CGRectGetMaxY(_topScrollView.frame), self.bounds.size.width, self.bounds.size.height - CGRectGetHeight(_topScrollView.frame));
    _lineView.frame = CGRectMake(0, _topScrollView.bounds.size.height, self.bounds.size.width, 0.5f);
    
    for (UIViewController *vc in _viewControllers) {
        NSUInteger index = [_viewControllers indexOfObject:vc];
        
        vc.view.frame = CGRectMake(_mainScrollView.bounds.size.width * index, 0, _mainScrollView.bounds.size.width, _mainScrollView.bounds.size.height);
    }
    
    _mainScrollView.contentSize = CGSizeMake(_mainScrollView.bounds.size.width * _viewControllers.count, _mainScrollView.bounds.size.height);
    
    [self scrollToIndex:_selectedIndex];
}

-(void)topAction:(UIButton *)btn
{
    if (_mainScrollView.scrollEnabled && _topScrollView.scrollEnabled) {
        NSUInteger tag = btn.tag - kSlideButtonBaseTag;
        [self scrollToIndex:tag];
    }
}

-(void)scrollToIndex:(NSUInteger)index
{
    if(_delegate && [_delegate respondsToSelector:@selector(slideView:willShowPageAtIndex:)])
    {
        [_delegate slideView:self willShowPageAtIndex:index];
    }
    if(_delegate && [_delegate respondsToSelector:@selector(slideView:willLeavePageAtIndex:)])
    {
        [_delegate slideView:self willLeavePageAtIndex:self.lastPageIndex];
    }
    
    self.lastPageIndex = index;
    
    //取消之前的选中状态
    UIButton *previousBtn = (UIButton *)[_topScrollView viewWithTag:kSlideButtonBaseTag + _selectedIndex];
    if (previousBtn) {
        [previousBtn setSelected:NO];
        previousBtn.titleLabel.font = _textFont;
    }

    self.selectedIndex = index;
    
    UIButton *selBtn = (UIButton *)[_topScrollView viewWithTag:kSlideButtonBaseTag + _selectedIndex];
    selBtn.titleLabel.font = [UIFont lx_boldSystemFontOfSize:17];
    [selBtn setSelected:YES];
    
//    NSString *title = selBtn.titleLabel.text;
//    CGRect textRect = [title boundingRectWithSize:CGSizeMake(1000.0f, 100.0f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.textFont} context:NULL];
    
    CGRect btnFrame = selBtn.frame;
    
    CGRect lineFrame = _topLineView.frame;
    CGFloat lineWidth = [LXAutoLength lx_L:30];
    lineFrame.origin.x = btnFrame.origin.x + (btnFrame.size.width - lineWidth) / 2.0f;
    lineFrame.size.width = lineWidth;
    
    [UIView animateWithDuration:0.3f animations:^{
        self.topLineView.frame = lineFrame;
    }];
    
    CGRect animateRect = CGRectMake(_selectedIndex * _mainScrollView.bounds.size.width, 0, _mainScrollView.bounds.size.width, _mainScrollView.bounds.size.height);
    
    [_mainScrollView scrollRectToVisible:animateRect animated:YES];
}

#pragma mark -UIScrollViewDelegate
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if(scrollView == _mainScrollView)
//    {
//        
//    }
//}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if(scrollView == _mainScrollView)
    {
        NSUInteger pageIndex = (scrollView.contentOffset.x + 5.0f) / scrollView.bounds.size.width;
        
        [self scrollToIndex:pageIndex];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
